package com.kucai.yifaqapp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.MsgBoxBean;
import com.kucai.yifaqapp.ui.fragment.gameFragment.MsgDetaiDialog;
import com.kucai.yifaqapp.util.OnItemClickListener;
import com.kucai.yifaqapp.util.SYSOUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import static com.kucai.yifaqapp.global.App.getContext;

public class MessageActivity extends BaseActivitySimple {
    private LinearLayout skinbg;
    private LinearLayout goback;
    private TextView headerTitle;
    private RecyclerView msglist;
    private SwipeRefreshLayout refreshLayout;
    private Adapter adapter;
    private int position = 1;
    private TextView more;
    private MsgBoxBean mdatas;
    private boolean isrefresh = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        skinbg = findViewById(R.id.skinbg);
        goback = findViewById(R.id.goback);
        headerTitle = findViewById(R.id.headerTitle);
        msglist = findViewById(R.id.msglist);
        refreshLayout = findViewById(R.id.swiprefresh);
        more = findViewById(R.id.more);
        headerTitle.setText("我的消息");
        goback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initDatas();
        refreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light, android.R.color.holo_orange_light);
        //给swipeRefreshLayout绑定刷新监听
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //开始访问网络数据
                isrefresh = true;
                initDatas();
            }
        });
        msglist.setLayoutManager(new LinearLayoutManager(this));
        adapter = new Adapter();
    }

    private void initDatas() {
        HttpJsonUtils.getHttpApiInstance()
                .get_user_messagebox(HttpAdress.getuseAdress() + "api/m/getUserMessageBox.json",Info.user.token, position, 20)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<MsgBoxBean>(this) {
                    @Override
                    protected void onCustomNext(MsgBoxBean resBanner) {
                        try {
                            if (isrefresh) {
                                isrefresh = false;
                                refreshLayout.setRefreshing(false);
                            } else {

                            }
                            mdatas = resBanner;
                            if (mdatas == null) {
                                more.setVisibility(View.VISIBLE);
                                refreshLayout.setVisibility(View.GONE);
                            } else {
                                if (mdatas.getData().size() != 0) {
                                    more.setVisibility(View.GONE);
                                    refreshLayout.setVisibility(View.VISIBLE);
                                    msglist.setAdapter(adapter);
                                } else {
                                    more.setVisibility(View.VISIBLE);
                                    refreshLayout.setVisibility(View.GONE);
                                }
                            }
                        } catch (Exception e) {

                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {

                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_message;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public Adapter.MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new Adapter.MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.msgitem, parent, false));

        }

        @Override
        public void onBindViewHolder(final Adapter.MViewHolder holder, final int position) {
            holder.text1.setText(mdatas.getData().get(position).getContent());
            holder.text2.setText(mdatas.getData().get(position).getAddDate());
            holder.item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MessageActivity.this, MsgDetaiDialog.class);
                    intent.putExtra("content", mdatas.getData().get(position).getContent());
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return mdatas.getData().size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView text1;
            TextView text2;
            LinearLayout item;

            MViewHolder(View itemView) {
                super(itemView);
                text1 = (TextView) itemView.findViewById(R.id.text1);
                text2 = itemView.findViewById(R.id.text2);
                item = itemView.findViewById(R.id.item);
            }
        }
    }
}
